package com.captain.leetcode;

//输入：s = "PAYPALISHIRING", numRows = 4
//输出："PINALSIGYAHRPI"
//解释：
//P     I    N
//A   L S  I G
//Y A   H R
//P     I
public class Z字形变换06 {

    public static void main(String[] args) {

        System.out.println(convert("PA", 1));
    }

    /**
     * @param s, numRows
     * @return java.lang.String
     * @author captain
     * @date 2021/7/9 22:24
     */
    public static String convert(String s, int numRows) {

        if (s == null || s.length() == 0 || numRows <= 1) return s;

        StringBuilder[] arr = new StringBuilder[numRows];
        for (int i = 0; i < numRows; i++) arr[i] = new StringBuilder();

        int index = 0;
        int flag = 1;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            arr[index].append(c);
            index += flag;
            if (index == numRows - 1 || index == 0) flag = -flag;
        }
        StringBuilder result = new StringBuilder();
        for (StringBuilder stringBuilder : arr) result.append(stringBuilder);
        return result.toString();
    }

}
